#include using namespace std; void main(){ int x = 3, y; // show address of x cout << &x << endl; // declare a pointer int *p; p = &x; cout << p << endl; // access x via pointer // "*p" is said to be a dereference cout << *p << endl; // change x via the pointer *p = 42; cout << x << endl; // put x into y y = *p; cout << y << endl; // declare a second pointer int *q; q = p; cout << q << endl; cout << *q << endl; // type mismatch // int z = p; will not compile // int *z = p; // z gets the address of x // int z = *p; // z gets the value of x // create a pointer to the address of p int **z = &p; cout << z << endl; // address of p cout << &p << endl; cout << *z << endl; // address x cout << **z << endl; // value of x // create an invalid pointer p = 0; // not allowed //cout << *p << endl; // reinitialize pointer p to a heap location p = new int(9); if(p != 0) cout << "successful allocation\n"; cout << p << endl; // address very different cout << *p << endl;// p points to heap delete(p); // do not dereference deleted memory cout << *p << endl;// bad idea p = 0;// invalidate pointer //cout << *p << endl; p = new int(77);// do a new allocation cout << *p << endl; delete(p); // allocate infinite memory for(int i=0; true; i++) p = new int();// memory leak - no delete }